﻿/* **
 * WebQ - a Web Questionare / Survey Framework
 * Andrew Adhi Ariane Murniadi / 2009
 * GPL
 * $Id$
 *									** */

using System;
using System.Collections.Generic;
using System.Text;

namespace WebQ.Core {
	/// <summary>
	/// ID generator for control client id. This class provides a predictable and reproduceable id 
	/// for answer fields so that the framework can extract user answers by reproducing the answer ids
	/// and extracting the values from the postback data.
	/// </summary>
	public class WebQIdGenerator {
		private String m_baseId = "";

		/// <summary>
		/// Base Id to ensure uniqueness of the id generated. Set this before generating an id
		/// </summary>
		/// <remarks>This property is subject to revision. Whether this need to be here or 
		/// should the client supply the baseId upon request of next Id is to be reviewed later</remarks>
		//Todo: Think about this
		public String BaseId {
			get { return m_baseId; }
			set { m_baseId = value; }
		}

		private const String m_answerPrefix = "answer";
		private const String m_separator = "_";

		private int m_seqNo = 0;

		/// <summary>
		/// Constructor
		/// </summary>
		public WebQIdGenerator () {
		}

		/// <summary>
		/// Produces next unique id
		/// </summary>
		/// <returns>Unique Id produced</returns>
		public virtual String GetNext () {
			String ret = m_baseId + m_separator + m_answerPrefix + m_separator + m_seqNo.ToString ();
			m_seqNo++;
			return ret;
		}

		/// <summary>
		/// Resets Id Generator back to init
		/// </summary>
		public virtual void Reset () {
			m_seqNo = 0;
		}
	}
}
